package com.cy.pj.sys.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.pj.common.pojo.Node;
import com.cy.pj.sys.dao.SysMenuDao;
import com.cy.pj.sys.dao.SysRoleMenuDao;
import com.cy.pj.sys.pojo.SysMenu;
@Service
public class SysMenuServiceImpl implements SysMenuService {
	@Autowired
	private SysMenuDao sysMenuDao;
	@Autowired
	private SysRoleMenuDao sysRoleMenuDao;
	@Override
	public List<Map<String, Object>> findObjects() {
		List<Map<String, Object>> list = sysMenuDao.findObjects();
		if(list==null || list.size()==0) {
			throw new ServiceException("没有对应的菜单信息");
		}
		return list;
	}
	@Override
	public int deleteObject(Integer id) {
		//参数校验
		if(id==null ||id<=0)
			throw new IllegalArgumentException("please choose one!");
		//判定是否有子菜单
		int count =sysMenuDao.getChildCount(id);
		//有子菜单先删除子菜单
		if(count>0)
			throw new ServiceException("请先删除子菜单");
		//执行菜单角色关系数据删除
		sysRoleMenuDao.deleteObjectsByMenuId(id);
		//删除自身信息
		int rows = sysMenuDao.deleteObject(id);
		//验证结果并返回
		if(rows==0)
			throw new ServiceException("this menus no exsits");
		return rows;
	}
	@Override
	public List<Node> findZtreeMenuNodes() {
		
		return sysMenuDao.findZtreeMenuNodes();
	}
	@Override
	public int saveObject(SysMenu entity) {
		int rows = sysMenuDao.insertObject(entity);
		return rows;
	}

}
